package com.richard.cloud;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richard.cloud.entity.Order;
import com.richard.cloud.feign.SeataAccountServiceFeign;
import com.richard.cloud.service.OrderService;
import com.richard.springcloud.base.ComnResult;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class BootSeataOrderService95 {
	public static void main(String[] args) {
		SpringApplication.run(BootSeataOrderService95.class, args);
	}
}

@Slf4j
@RestController
class TestCtrller {
	@Resource
	private OrderService service;
	@Resource
	private SeataAccountServiceFeign accountService;
	
	@GetMapping("/hello")
	public String hello() {
		return "order-service " + LocalDateTime.now().toString();
	}
	
	//feign test
	@GetMapping("/test/feign-account")
	public String test_feign_account() {
		return accountService.hello();
	}
	
	/**
	 * 开始测试@GlobalTransactional
	 * 创建订单-初始化，扣减商品库存，扣减账户余额，更新订单状态
	 * @param prodId 商品ID
	 * @param count 商品数量
	 * @param price 单价
	 * @param custId 客户ID
	 * @return
	 */
	@GetMapping("/order/create/seata")
	public ComnResult<?> testSeata(@RequestParam Long prodId, @RequestParam Integer count,
								@RequestParam BigDecimal price, @RequestParam Long custId){
		log.info(">> 订单创建开始");
		Order save = service.createOrder(new Order(custId, prodId, count, price.multiply(new BigDecimal(count))));
		log.info(">> 订单创建结束");
		return ComnResult.succ(save.getOrderNo());
	}
}